<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增活动')" />
    <th:block th:include="include :: datetimepicker-css" />
<!--    <th:block th:include="include :: summernote-css" />-->
    <th:block th:include="include :: bootstrap-fileinput-css" />
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap-select/1.10.0/css/bootstrap-select.min.css" rel="stylesheet">
    <style>
        .test-textarea {
            width: 400px;
            min-height: 26px;
            line-height: 20px;
            _height: 30px;
            /* max-height: 150px;*/
            margin-left: auto;
            margin-right: auto;
            padding: 3px;
            outline: 0;
            border: 1px solid #ccc;
            font-size: 12px;
            word-wrap: break-word;
            overflow-x: hidden;
            overflow-y: auto;
            -webkit-user-modify: read-write-plaintext-only;
            border-radius: 4px;
        }
    </style>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-activity-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">活动主题：</label>
                <div class="col-sm-8">
                    <input id="activityTitle" name="activityTitle" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">活动内容：</label>
                <div class="col-sm-8">
                    <!--<input type="textarea" class="form-control" name="activityPcontent">-->
                    <textarea id="activityPcontent" name="activityPcontent" cols="5" class="form-control" required></textarea>
                   <!-- <div class="summernote" id="activityContent"></div>-->
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">活动图片</label>
                <div class="col-sm-8">
                    <!--<input id="fileinput-demo-1" type="file" multiple>-->
                    <div class="file-loading">
                        <input id="fileinput-demo-1" type="file" multiple data-min-file-count="1" data-theme="fas" required>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">活动发布：</label>
                <div class="col-sm-8">
                    <select class="selectpicker form-control" onchange="initCity()" id="activityProvince" name="activityProvince">
                        <option value="">--省份--</option>
                    </select>
                    <select class="selectpicker form-control" onchange="initRegion()" id="activityCity" name="activityCity">
                        <option value="">--城市--</option>
                    </select>
                    <select class="selectpicker form-control" id="activityDist" name="activityDist">
                        <option value="">--县区--</option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">活动地点：</label>
                <div class="col-sm-8">
                    <input id="activityAddress" name="activityAddress" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">活动可参与人数：</label>
                <div class="col-sm-8">
                    <input id="activityMember" name="activityMember" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">活动费用：</label>
                <div class="col-sm-8">
                    <input id="activityPrice" name="activityPrice" class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
<!--    <th:block th:include="include :: summernote-js" />-->
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script src="https://cdn.bootcdn.net/ajax/libs/bootstrap-select/1.10.0/js/bootstrap-select.min.js"></script>
    <script th:inline="javascript">
        var prefix = ctx + "lm/activity";

        $(document).ready(function(){
            // 地区下拉框初始化
            $("#activityProvince").selectpicker({width:130, liveSearch: true, liveSearchPlaceholder: "搜索" });
            $("#activityCity").selectpicker({width:120, liveSearch: true, liveSearchPlaceholder: "搜索" });
            $("#activityDist").selectpicker({width:120, liveSearch: true, liveSearchPlaceholder: "搜索" });

            // 动态加载省份选项数据
            initSelectOptions("activityProvince","0");
        });// onReady

        /**
         * 动态生成select选项
         * @param selectId
         * @returns
         */
        function initSelectOptions(selectId,parentId) {
            var selectObj = $("#" + selectId);
            $.ajax({
                url : prefix+"/"+parentId,
                async : false,
                type : "GET",
                success : function(result) {

                    var configs = result.rows;
                    selectObj.find("option:not(:first)").remove();
                    for (var i in configs) {
                        var addressConfig = configs[i];
                        var optionValue = addressConfig.id;
                        var optionText = addressConfig.areaName;
                        selectObj.append(new Option(optionText, optionValue));
                    }
                    debugger;
                    // 刷新select
                    selectObj.selectpicker('refresh');
                },
                error : function(result) {
                    toastr.error('获取['+ parentId + ']信息失败，原因：' + result.errorMessage);
                }
            });// ajax
        }

        /**
         * 根据选择的省份动态初始化城市options
         * @returns
         */
        function initCity() {
            // 当省份变动时，初始化城市和清空县区和街道
            var provinceSel = $("#activityProvince").val();
            initSelectOptions("activityCity", provinceSel);
            $("#activityDist").find("option:not(:first)").remove();
            $("#activityDist").selectpicker('refresh');
            /* $("#street").find("option:not(:first)").remove();
             $("#street").selectpicker('refresh');*/
        }

        /**
         * 根据选择的城市动态初始化区域options
         * @returns
         */
        function initRegion() {
            // 选择城市
            var citySel = $("#activityCity").val();
            initSelectOptions("activityDist", citySel);
        }



        $(document).ready(function () {
            debugger;
            $("#fileinput-demo-1").fileinput({
                'uploadUrl': '#',
                overwriteInitial: true,
                initialPreviewAsData: true,
                showUpload:false,
                initialPreview: [

                ]
            });
        });

        $("#form-activity-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                uploadFile();
            }
        }

        function uploadFile() {
            debugger;
            var formData = new FormData();
            if($('#fileinput-demo-1')[0].files.length>0){
                formData.append('fileName', $("#fileinput-demo-1").val());
                for(var i=0;i<$('#fileinput-demo-1')[0].files.length;i++){
                    formData.append('files', $('#fileinput-demo-1')[0].files[i]);
                }
            }
            formData.append('activityTitle',$("#activityTitle").val());
            formData.append('activityPcontent',$("#activityPcontent").val());
            formData.append('activityAddress',$("#activityAddress").val());
            formData.append('activityMember',$("#activityMember").val());
            formData.append('activityPrice',$("#activityPrice").val());
            formData.append('activityProvince',$("#activityProvince").val());
            formData.append('activityCity',$("#activityCity").val());
            formData.append('activityDist',$("#activityDist").val());

            $.ajax({
                url: prefix + "/add",
                type: 'post',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                dataType: "json",
                success: function(result) {
                    $.operate.successCallback(result);
                }
            });
        }

        $("input[name='reviewTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

       /* $(function() {
            $('.summernote').summernote({
                lang: 'zh-CN',
                callbacks: {
                    onChange: function(contents, $edittable) {
                        $("input[name='" + this.id + "']").val(contents);
                    },
                    onImageUpload: function(files) {
                    	var obj = this;
                    	var data = new FormData();
                    	data.append("file", files[0]);
                    	$.ajax({
                    		type: "post",
                    		url: ctx + "common/upload",
                    		data: data,
                    		cache: false,
                    		contentType: false,
                    		processData: false,
                    		dataType: 'json',
                    		success: function(result) {
                    			if (result.code == web_status.SUCCESS) {
                    				$('#' + obj.id).summernote('insertImage', result.url);
                    			} else {
                    				$.modal.alertError(result.msg);
                    			}
                    		},
                    		error: function(error) {
                    			$.modal.alertWarning("图片上传失败。");
                    		}
                    	});
                    }
                }
            });
        });*/
    </script>
</body>
</html>